package edu.ncst.baseweb.controller.equ;

import cn.wintersun.basecommon.common.Result;
import edu.ncst.baseweb.dao.equ.Equ;
import edu.ncst.baseweb.dao.equ.EquClass;
import edu.ncst.baseweb.service.intrer.equ.EquService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * 设备控制器
 * 设备详情管理
 */
@RestController
@RequestMapping("admin/equipment/equ")
@PreAuthorize("hasAuthority('3')")
public class EquController {

    @Autowired
    private EquService equService;

    @RequestMapping(value = "list",method = RequestMethod.POST)
    public Result<IPage<Equ>> list(@RequestBody Equ equ){

        return  equService.list(equ);
    }
    //添加或编辑
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public Result<Object> save(@RequestBody Equ equ){
        try {
            return  equService.save(equ);
        } catch (Exception e) {
            //抛出异常
            return Result.failure(500, e.getMessage());
        }
    }
    @RequestMapping(value = "getClassName",method = RequestMethod.GET)
    public Result<List<EquClass>> getClassName(){

        return equService.getClassName();
    }
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public Result<Object> delete(@RequestBody String equId){
        try {
            return equService.delete(equId);
        } catch (Exception e) {
            return Result.failure(500, e.getMessage());
        }
    }
    //上传图片

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("image") MultipartFile image,@RequestParam("id") Integer id) {

        // 处理图片上传逻辑
        if (!image.isEmpty()) {
            return equService.uploadFile(image,id);
        } else {
            return "请选择要上传的图片";
        }
    }
}
